//
// Created by feng on 2020/6/29.
//

#include "com_kxf_bsdiff_jni_BsDiffUtil.h"

/*
 */
JNIEXPORT jstring JNICALL Java_com_kxf_bsdiff_jni_BsDiffUtil_getInfo
        (JNIEnv *env, jclass jc) {
    return (*env)->NewStringUTF(env, "{\"name\": \"bsdiffutil\",\"versionName\": \"1.0.0\",\"versionCode\": \"1\"}");
}

JNIEXPORT jint JNICALL
Java_com_kxf_bsdiff_jni_BsDiffUtil_diff(JNIEnv *env, jclass clazz, jstring oldpath, jstring newpath,
                                        jstring patch) {
    char *argv[4];
    argv[0] = "bsdiff";
    argv[1] = (*env)->GetStringUTFChars(env, oldpath, 0);
    argv[2] = (*env)->GetStringUTFChars(env, newpath, 0);
    argv[3] = (*env)->GetStringUTFChars(env, patch, 0);
    LOGD(TAG,"Java_com_kxf_bsdiff_jni_BsDiffUtil_diff 111", 111);
    bsdiff_main(4, (char *)argv);
    LOGD(TAG,"Java_com_kxf_bsdiff_jni_BsDiffUtil_diff 222", 222);
    (*env)->ReleaseStringUTFChars(env, oldpath, argv[1]);
    (*env)->ReleaseStringUTFChars(env, newpath, argv[2]);
    (*env)->ReleaseStringUTFChars(env, patch, argv[3]);
    LOGD(TAG,"Java_com_kxf_bsdiff_jni_BsDiffUtil_diff 333", 333);
//    free(argv);//释放free(argv[0]);代码就要嗝屁了
    free(argv[1]);
    free(argv[2]);
    free(argv[3]);
    LOGD(TAG,"Java_com_kxf_bsdiff_jni_BsDiffUtil_diff 444", 444);
    return 0;
}

JNIEXPORT jint JNICALL
Java_com_kxf_bsdiff_jni_BsDiffUtil_patch(JNIEnv *env, jclass clazz, jstring oldpath,
                                         jstring newpath, jstring patch) {
    char *argv[4];
    argv[0] = "bspatch";
    argv[1] = (*env)->GetStringUTFChars(env, oldpath, 0);
    argv[2] = (*env)->GetStringUTFChars(env, newpath, 0);
    argv[3] = (*env)->GetStringUTFChars(env, patch, 0);
    LOGD(TAG,"Java_com_kxf_bsdiff_jni_BsDiffUtil_patch 111", 111);
    bspatch_main(4, (char *)argv);
    (*env)->ReleaseStringUTFChars(env, oldpath, argv[1]);
    (*env)->ReleaseStringUTFChars(env, newpath, argv[2]);
    (*env)->ReleaseStringUTFChars(env, patch, argv[3]);
//    free(argv);
    free(argv[1]);
    free(argv[2]);
    free(argv[3]);
    return 0;
}


int strsplit(const char *str, char *parts[], const char *delimiter) {
    char *pch;
    int i = 0;
    char *copy = NULL, *tmp = NULL;

    copy = strdup(str);
    if (!copy)
        goto bad;

    pch = strtok(copy, delimiter);

    tmp = strdup(pch);
    if (!tmp)
        goto bad;

    parts[i++] = tmp;

    while (pch) {
        pch = strtok(NULL, delimiter);
        if (NULL == pch) break;

        tmp = strdup(pch);
        if (!tmp)
            goto bad;

        parts[i++] = tmp;
    }

    free(copy);
    return i;

    bad:
    free(copy);
    for (int j = 0; j < i; j++)
        free(parts[j]);
    return -1;
}

JNIEXPORT jint JNICALL
Java_com_kxf_bsdiff_jni_BsDiffUtil_executeBZ2Command(JNIEnv *env, jclass clazz, jstring command) {
    const char *command1 = (*env)->GetStringUTFChars(env, command, 0);
    char *parts[8];
    size_t size = strsplit(command1, parts, " ");
    LOGD(TAG,"command = %s size = %zu", command1,size);
    char *argv[size];
    for (int i = 0; i < size; ++i) {
        argv[i] = parts[i];
    }
    int ret = bz2_main(size, argv);
    __android_log_print(ANDROID_LOG_INFO,"bz2_command", "ret = %d", ret);
    // TODO
    (*env)->ReleaseStringUTFChars(env, command, command1);
    return ret;
}
